package com.lingdong.onlinejudge.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lingdong.onlinejudge.annotation.AuthCheck;
import com.lingdong.onlinejudge.common.BaseResponse;
import com.lingdong.onlinejudge.common.ErrorCode;
import com.lingdong.onlinejudge.common.ResultUtils;
import com.lingdong.onlinejudge.exception.BusinessException;
import com.lingdong.onlinejudge.model.dto.user.UserListDto;
import com.lingdong.onlinejudge.model.dto.user.UserPageListDto;
import com.lingdong.onlinejudge.model.vo.user.UserUpdateDto;
import com.lingdong.onlinejudge.model.vo.user.UserVo;
import com.lingdong.onlinejudge.service.UserService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 用户相关接口
 * @author lzw
 */

@RestController
@RequestMapping("/user")
public class UserController {

    @Resource
    private UserService userService;

    /**
     * 修改用户信息
     * @param userUpdateDto 修改用户信息
     * @return 结果
     */
    @PutMapping("/update")
    public BaseResponse<Boolean> updateUser(@RequestBody UserUpdateDto userUpdateDto){
        if (userUpdateDto == null){
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        Boolean result = userService.updateUser(userUpdateDto);
        return ResultUtils.success(result);
    }

    /**
     * 查询用户信息
     * @param id 用户id
     * @return 用户信息
     */
    @GetMapping("/query/{id}")
    public BaseResponse<UserVo> queryUser(@PathVariable Long id){
        if (id == null){
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        UserVo userVo = userService.queryUser(id);
        return ResultUtils.success(userVo);
    }

    /**
     * 分页查询用户列表
     * @param pageListDto 分页信息
     * @return 用户列表
     */
    @PostMapping("/query/page")
    public BaseResponse<Page<UserVo>> queryUserByPage(@RequestBody UserPageListDto pageListDto){
        if (pageListDto == null){
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        Page<UserVo> userVoList = userService.queryUserByPage(pageListDto);
        return ResultUtils.success(userVoList);
    }

    /**
     * 查询用户列表
     * @param userListDto 查询用户列表
     * @return 用户列表
     */
    @PostMapping("/query/list")
    public BaseResponse<List<UserVo>> queryUserList(@RequestBody UserListDto userListDto){
        if (userListDto == null){
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        List<UserVo> userVoList = userService.queryUserList(userListDto);
        return ResultUtils.success(userVoList);
    }

    /**
     * 批量删除用户
     * @param idList 用户id列表
     * @return 结果
     */
    @AuthCheck(mustRole = "admin")
    @DeleteMapping("/delete")
    public BaseResponse<Boolean> deleteUser(@RequestBody List<Long> idList){
        if (idList == null){
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        Boolean result = userService.deleteUser(idList);
        return ResultUtils.success(result);
    }
}

